const {prisma, minioClient, mqttClient}=require('./utils.js')

class OrganizationController{
    /**
     * GET:获取机构树
     * /organization
     * @param {*}
     * @returns
     * 500 内部错误
     * 200 机构树
     * [
     *   {
     *     id:xx, 
     *     name:xx, 
     *     level2:[
     *       {
     *         id:xx,
     *         name:xx,
     *         organizations:[
     *           {
     *             organization_type:xx,
     *             id_code:xx
     *             name:xx,
     *             address:xx,
     *             introduce:xx,
     *             photos:[xx,xx],
     *             visit_count:xx
     *           },
     *           ...
     *         ]
     *       },
     *       ...
     *     ]
     *   },
     *   ...
     * ]
     */
    async all(req,res){
        try{
            var ret=await prisma.level1.findMany({
                select:{
                    id:true,
                    name:true,
                    level2:{
                        select:{
                            id:true,
                            name:true,
                            organizations:true
                        }
                    }
                }
            })
            res.send(200,ret)
        }catch(err){
            console.error(err)
            res.send(500)
        }
    }

    /**
     * GET：获取机构信息
     * /organization/:id
     * @param {int} id
     * @returns
     * 500 服务器内部错误
     * 404 未找到机构
     * 200 organization
     */
    async one(req,res){
        var id=Number(req.params.id)
        try{
            var ret=await prisma.organization.findUnique({
                where:{id:id},
                include:{
                    level2s:{
                        include:{level1:true}
                    },
                    products:true,
                    services:true,
                    publised_demonds:true,
                    unveiled_demonds:true
                }
            })
            if(ret==null){
                res.send(404)
                return
            }
            res.send(200,ret)
        }catch(err){
            console.error(err)
            res.send(500)
        }
    }
}

module.exports=new OrganizationController()